package com.cheng.controller;

import com.cheng.pojo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
/*
 * @RequestMapping:注解放在类上给所有控制器方法设置url模块
 * */
@RequestMapping(value = "/user",produces = "textext/plain;charset=UTF-8")
public class UserController {

    /*
     * @RequestMapping：放在控制器方法上，value给控制器方法设置url请求地址,
     *   method设置请求方式。不写表示Get和Post方式都可以控制
     * */
    @RequestMapping(value = "/loginGet.mvc", method = RequestMethod.GET)
    public ModelAndView loginGet(@RequestParam("userName") String name, @RequestParam("userPwd") String pwd) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userName", name);
        modelAndView.addObject("userPwd", pwd);

        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping(value = "/loginPost.mvc", method = RequestMethod.POST)
    public ModelAndView loginPost(User user) {
        ModelAndView modelAndView = new ModelAndView();
        /*向视图传递的值*/
        modelAndView.addObject("userName", user.getUserName());
        modelAndView.addObject("userPwd", user.getUserPwd());
        modelAndView.addObject("user", user);
        /*转发到指定的视图*/
        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping(value = "/login.mvc")
    public ModelAndView login(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session) {
        User user = new User();
        user.setUserName(request.getParameter("userName"));
        user.setUserPwd(request.getParameter("userPwd"));

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userName", user.getUserName());
        modelAndView.addObject("userPwd", user.getUserPwd());
        modelAndView.addObject("user", user);

        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping(value = "/toInfo.mvc")
    public String toInfo(HttpServletRequest request, Model model) {
        model.addAttribute("name", "Join in InfoPage Method ModelSetAttribute");
//        request.setAttribute("name", "Join in InfoPage");
        return "info";
    }

    @RequestMapping(value = "/returnVoid.mvc")
    public void returnVoid(HttpServletResponse response) {
        System.out.println("Join GetJson Method");
        PrintWriter out = null;
        List<User> list = new ArrayList<>();
        try {
            out = response.getWriter();

            for (int i = 1; i < 10; i++) {
                User user = new User();
                user.setUserName("用户" + i);
                user.setUserPwd("1000" + i + 1);

                list.add(user);
            }

            response.setContentType("application/json");
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(list);
            out.print(json);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.close();
    }

    @RequestMapping(value = "/returnObject.mvc")
    @ResponseBody
    public User returnObject() {
        User user = new User();
        user.setUserName("用户");
        user.setUserPwd("asd123");
        return user;
    }

    @RequestMapping(value = "doString.mvc")
    @ResponseBody
    public String doString() {
        return "tip:Hello Spring MVC测试";
    }
}
